package cn.lic.tool.poi.excel.function.impl;

import org.apache.poi.ss.usermodel.Cell;
import com.alibaba.fastjson.JSONObject;

import cn.lic.tool.poi.excel.ParserUtils;
import cn.lic.tool.poi.excel.function.AbstractFunction;
import cn.lic.tool.poi.excel.function.IFunction;
import cn.lic.tool.poi.excel.function.IFunctionArgument;
import cn.lic.tool.poi.excel.function.IValue;
import cn.lic.tool.poi.excel.function.IValue.Type;
import cn.lic.tool.poi.excel.function.Range;

/**
 * 连接字符串
 * @author T_lichuang1_kzx
 *
 */
public class Concat extends AbstractFunction {
	
	public Concat(){};
	
	public Concat(IFunctionArgument[] args) {
		this.args = args;
	}

	@Override
	public IFunction build(IFunctionArgument[] args) {
		return new Concat(args);
	}

	@Override
	public IValue execute(JSONObject ctx) {
		String split = this.getArg(0, ctx).castValue(Type.STRING, "");
		StringBuffer sb = new StringBuffer();
		for (int i = 1; i < args.length; i++) {
			String str = this.getArg(i, ctx).castValue(Type.STRING, "");
			sb.append(str);
			sb.append(split);
		}
		String rs = sb.substring(0, sb.length() - split.length());
		return IValue.buildSValue(rs);
	}

	@Override
	public Range execute(JSONObject ctx, Cell cell) {
		IValue val = this.execute(ctx);
		ParserUtils.setCellValue(cell, val);
		return null;
	}
}
